﻿$(document).ready(function (){
	$("#registerr").hide();
	$("#choose").hide();
	$("#submit").click(function(){
		var username= $("#username").attr("value");
		var password= $("#password").attr("value");
		$.ajax({
			type:"GET",
			url:"index/login/username/"+username+"/password/"+password,
			async:true,
			beforeSend:function loading(){
			},
			success:function(v){
				var data = window.JSON.parse(v);
				if(!data.success) {
					alert(data.message);
				} else {
					if(data.hasrobot)
						window.location.href="alanstreet.html";
					else {						
//						$("#registerr").hide();
//						$("#login").hide();
//						$("#choose").show();
						window.location.href="choose.html";
					}
				}
			},
			error:function(){
			}
		});
	});	
	
	$("#signupsubmit").click(function(){
		var usernamesignup= $("#usernamesignup").attr("value");
		var emailsignup= $("#emailsignup").attr("value");
		var passwordsignup= $("#passwordsignup").attr("value");
		var passwordsignup_confirm= $("#passwordsignup_confirm").attr("value");
		if(isNull(usernamesignup) || isNull(emailsignup) || isNull(passwordsignup) || isNull(passwordsignup_confirm)) {
			alert("Please check, fields should not be null");
			return;
		}
		if(isBlank(usernamesignup) || isBlank(emailsignup) || isBlank(passwordsignup) || isBlank(passwordsignup_confirm)){
			alert("Please check, should not use blank character");
			return;
		}
		if(passwordsignup != passwordsignup_confirm) {
			alert("Passwords are not the same!");
			return;
		}
        
		if(passwordsignup == passwordsignup_confirm){
			$.ajax({
				type:"GET",
				url:"index/register/username/"+usernamesignup+"/email/"+emailsignup+"/password/"+passwordsignup,
				async:true,
				beforeSend:function loading(){
				},
				success:function(v){
					var data = window.JSON.parse(v);
					if(!data.success) {
						alert(data.message);
					} else {
						window.location.href="choose.html";
					}
				},
				error:function(){
				}
			});		
		}else{
			alert("Plz input password again! Don't match!");
		}		
	});	
	$("#toregister").click(function(){
		
		$("#login").hide(500);
		
		$("#registerr").show(500);
			
	});	
	$("#tologin").click(function(){
		
		$("#login").show(500);
		
		$("#registerr").hide(500);
			
	});	
});

function isNull(val)
{
	return(val==null || typeof(val)=="undefined");
}

function isBlank(val){
	if(isNull(val))
		return true;
	val += "";
	for(var i=0;i<val.length;i++) {
		switch(val.charAt(i))
		{
			case " ":
			case "\t":
			case "\n":
			case "\r":
				break;
			default:
				return false;
		}
	}
	return true;
}